package com.xinyuan.service;

import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.IOException;

/**
 * @Classname 对高版本Excel进行读写、修改等操作
 * @Description TODO
 * @Date 2020/5/21 18:18
 * @Created by zfl
 */
public class GetExcelInfo {

    public void readExcel(File file) throws IOException {
        XSSFWorkbook workbook=new XSSFWorkbook(FileUtils.openInputStream(file));
        //两种方式读取工作表
        // Sheet sheet=workbook.getSheet("Sheet0");
        Sheet sheet= (Sheet) workbook.getSheetAt(0);
        //获取sheet中最后一行行号
        int lastRowNum=sheet.getLastRowNum();
        for (int i=0;i<=lastRowNum;i++){
            Row row=sheet.getRow(i);
            //获取当前行最后单元格列号
            int lastCellNum=row.getLastCellNum();
            for (int j=0;j<lastCellNum;j++){
                Cell cell=row.getCell(j);
                cell.setCellType(CellType.STRING);
                String value=cell.getStringCellValue();
                System.out.print(value+" ");
            }
            System.out.println();
        }
        workbook.close();
    }
}
